package com.nulldev.util.web.HttpServer3.extensions.nCaptcha.captchas.obftext;

import java.awt.image.BufferedImage;

import com.nulldev.util.VariableAPI.RandomUtil;
import com.nulldev.util.VariableAPI.StringsUtil;
import com.nulldev.util.data.Images.ImageUtil;
import com.nulldev.util.data.Images.legacy.TextObf;
import com.nulldev.util.data.Images.legacy.TextObf.ObfuscationMethod;
import com.nulldev.util.data.Sets.DataSet;
import com.nulldev.util.web.HttpServer3.extensions.nCaptcha.backend.Captcha;
import com.nulldev.util.web.HttpServer3.pages.v3.HttpPage3.Request;

public class MathCaptcha implements Captcha {

	@Override
	public DataSet<String, String[]> generateBase64Image(final int botLevel, final Request request) {
		// polishing my 11+4's
		final int i0 = RandomUtil.randInt(1, 11);
		final int i1 = RandomUtil.randInt(1, 11);
		final String spaces = StringsUtil.repeat(" ", RandomUtil.randInt(1, 3));
		final String str = i0 + spaces + "+" + spaces + i1 + spaces + "=" + spaces + "?";
		final BufferedImage obf = TextObf.obfuscate(str,
				(botLevel > 1 ? ObfuscationMethod.V2_EXPERIMENT_ONE : ObfuscationMethod.RainbowBackgroundNoise),
				TextObf.__font(BASE_TEXT_SIZE * 2));
		final String base64 = ImageUtil.encodeToString(obf, "png");
		return new DataSet<String, String[]>("data:image/png;base64," + base64, new String[]
			{ String.valueOf(i0 + i1), String.valueOf(i0 + i1) + ".0" });
	}

	@Override
	public String getType() {
		return "math";
	}

}
